#!/bin/sh

echo "please input your tincnet ip:"
read ip
echo "please input you netmask:"
read ipmask



#calc the ip and mask                                                       
                                                                            
mask2cdr ()                                                                 
{                                                                           
                                                                            
   local x=${1##*255.}                                                      
   set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) ${x%%.*}
   x=${1%%$3*}                                                              
   echo $(( $2 + (${#x}/4) ))                                               
#   bitmask=$(( $2 + (${#x}/4) ))                                            
}                                                                           
cdr2mask ()                                                                 
{                                                                           
   # Number of args to shift, 255..255, first non-255 byte, zeroes          
   set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
   [ $1 -gt 1 ] && shift $1 || shift                                                    
   echo ${1-0}.${2-0}.${3-0}.${4-0}                                                     
}                          

bitmask=$(mask2cdr $ipmask)

get_netdes()
{
ip1=$(echo ${ip} | awk -F "." '{print $1}')
ip2=$(echo ${ip} | awk -F "." '{print $2}')
ip3=$(echo ${ip} | awk -F "." '{print $3}')
ip4=$(echo ${ip} | awk -F "." '{print $4}')
echo "$ip1 $ip2 $ip3 $ip4"
mask1=$(echo ${ipmask} | awk -F "." '{print $1}')
mask2=$(echo ${ipmask} | awk -F "." '{print $2}')
mask3=$(echo ${ipmask} | awk -F "." '{print $3}')
mask4=$(echo ${ipmask} | awk -F "." '{print $4}')
echo "$mask1 $mask2 $mask3 $mask4"

var=1
var=$[$var+1]
gate1=$(($ip1&$mask1))
gate2=$(($ip2&$mask2))
gate3=$(($ip3&$mask3))
gate4=$(($ip4&$mask4))
echo "$gate1 $gate2 $gate3 $gate4"
VICNET=$(echo $gate1.$gate2.$gate3.$gate4)
}
get_netdes

tincfolder="/etc/tinc/tincnet"
if [ ! -d "$tincfolder" ]; then

    mkdir -p "$tincfolder"/hosts

fi


mac=$(ifconfig | grep br-lan | awk '{ print $5 }'| tr A-Z a-z| sed 's/://g')



echo "please input your tinc server's name:"
read servernode
echo "please input your tinc server's port:" 
read port
#make tinc files

echo "Name = tinc${mac}" >> /etc/tinc/tincnet/tinc.conf
echo "Interface = tinctun" >> /etc/tinc/tincnet/tinc.conf
echo "AddressFamily = ipv4" >> /etc/tinc/tincnet/tinc.conf
echo "Mode = switch" >> /etc/tinc/tincnet/tinc.conf
echo "ConnectTo = ${servernode}" >> /etc/tinc/tincnet/tinc.conf
echo "Compression=9" >> /etc/tinc/tincnet/tinc.conf
echo "Cipher = aes-256-cbc" >> /etc/tinc/tincnet/tinc.conf
echo "Digest = sha256" >> /etc/tinc/tincnet/tinc.conf
echo "PrivateKeyFile=/etc/tinc/tincnet/rsa_key.priv" >> /etc/tinc/tincnet/tinc.conf


echo "Subnet = ${ip}/32" >> /etc/tinc/tincnet/hosts/tinc${mac}
echo "Port = ${port}" >> /etc/tinc/tincnet/hosts/tinc${mac} 

echo "#!/bin/sh" >> /etc/tinc/tincnet/tinc-up
echo 'ip link set $INTERFACE up' >> /etc/tinc/tincnet/tinc-up
echo 'ip addr add '${ip}'/'${bitmask}' dev $INTERFACE' >> /etc/tinc/tincnet/tinc-up
echo 'ip route add '${VICNET}'/'${bitmask}' dev $INTERFACE' >> /etc/tinc/tincnet/tinc-up


echo "#!/bin/sh" >> /etc/tinc/tincnet/tinc-down
echo 'ip route del '${VICNET}'/'${bitmask}' dev $INTERFACE' >> /etc/tinc/tincnet/tinc-down        
echo 'ip addr del '${ip}'/'${bitmask}' dev $INTERFACE' >> /etc/tinc/tincnet/tinc-down
echo 'ip link set $INTERFACE down' >> /etc/tinc/tincnet/tinc-down


sed -i "s/option enabled 0/option enabled 1/g"  /etc/config/tinc
sed -i "s/NETNAME/tincnet/g" /etc/config/tinc
sed -i "s/NODENAME/tinc${mac}/g" /etc/config/tinc


chmod 0755 /etc/tinc/tincnet/tinc* 
